package com.adcc.zkdemo.web;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import javax.sql.DataSource;
import javax.validation.ValidatorFactory;
import java.beans.PropertyVetoException;
import java.util.Properties;

@Configuration
@ComponentScan(basePackages = {"com.adcc.zkdemo"})
@EnableJpaRepositories("com.adcc.zkdemo")
@EnableTransactionManagement
public class ApplicationContext {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationContext.class);
	
	@Bean(destroyMethod = "close")
	public DataSource dataSource() {
		ComboPooledDataSource ds = new ComboPooledDataSource();
		try {
			ds.setDriverClass("org.postgresql.Driver");
			ds.setJdbcUrl("jdbc:postgresql://127.0.0.1:5432/zkdemo");
			ds.setUser("efb");
			ds.setPassword("efb");
		} catch (PropertyVetoException e) {
			LOGGER.error(e.getLocalizedMessage());
			return null;
		}
		return ds;
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
		emf.setDataSource(dataSource());
		emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
		emf.setPackagesToScan("com.adcc.zkdemo");
		Properties p = new Properties();
		// ����������ץȡ���������ȣ�ȡֵ. ��������Ϊ0��3֮��
		// hibernate.max_fetch_depth=0ʱ����ʾ�����������ӣ���ʱ��Ԥ��ץȡ���Ծ���������Ҳ��ʧЧ�ģ�ȡ���֮�����������ز���
		// p.put("hibernate.max_fetch_depth",
		// env.getProperty("hibernate.max_fetch_depth"));
		// Hibernate���ԣ�Dialect�������� - ������Hibernateʹ��ĳЩ�ض�����ݿ�ƽ̨������
		p.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
		// ��ʽ��hibernate��SQL������
		p.put("hibernate.format_sql", "true");
		// �����е�SQL��䶼���������̨(������Ϊlog���ܵ�һ�����). ȡֵ. true | false
		p.put("hibernate.show_sql", "true");
		// �Զ����schema����DDL���. ȡֵ. update | create | create-drop
		p.put("hibernate.hbm2ddl.auto", "update");
		p.put("auditor-aware-ref", "springSecurityAuditorAware");
		emf.setJpaProperties(p);
		return emf;
	}

	@Bean
	public ValidatorFactory validator() {
		return new LocalValidatorFactoryBean();
	}
	
	@Bean
	public JpaTransactionManager transactionManager() {
		JpaTransactionManager txm = new JpaTransactionManager();
		txm.setEntityManagerFactory(entityManagerFactory().getObject());
		return txm;
	}

}
